package Integration;

import java.util.ArrayList;

import jmeshless.NewOkCancelDialog;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import FEMShapeFunction1D.FEMShapeFunction1D1;
import MeshlessShapeFunctions.JCubicSpline;
import MeshlessShapeFunctions.JGaussian;
import MeshlessShapeFunctions.JMeshlessInterpolationFunction1D;
import MeshlessShapeFunctions.JMeshlessInterpolationFunction1DCoupled2;
import MeshlessShapeFunctions.JStKernel;
import MeshlessShapeFunctions.Particle;

public class derivavty {
	protected IFunction1D func;
	public static double h = 0.0001; 
	
	public static double calculate(double x,IFunction1D function) {
		return ( function.calculate(x) - function.calculate(x-h)) / h;
	}
	
	public double calculate2(double x) {
		return ( func.calculate(x+h) - 2*func.calculate(x)+func.calculate(x-h)) / h/h;
	}
	
	public derivavty(IFunction1D func) {
		this.func = func;
	}
	
	public double calculate(double x){
		return ( func.calculate(x) - func.calculate(x-h)) / h;
	}
	
	
	public static void main(String[] args){
		
		double a = 0.0,b=1;
        double x = a;


        int nn = 10;
        ArrayList<Particle> ls1D = new ArrayList<Particle>();
        double h = (b-a)/(double)nn;
        for (int i = 0; i <=nn; i++) {
            ls1D.add(new Particle(a+h*i, 0, i));
        }

        ArrayList<JMeshlessInterpolationFunction1D> ls = new ArrayList<JMeshlessInterpolationFunction1D>();

        for (int i = 0; i < ls1D.size(); i++) {
            Particle particle = ls1D.get(i);
            JMeshlessInterpolationFunction1D f = new JMeshlessInterpolationFunction1D(2, particle.getX(), 0.5, new JCubicSpline(0.5));
            ls.add(f);
        }

        for (int i = 0; i < ls1D.size(); i++) {
            JMeshlessInterpolationFunction1D f = ls.get(i);
            f.setNodeList(ls1D);
        }
        
        JMeshlessInterpolationFunction1D jMeshlessInterpolationFunction1D = ls.get(5);
        x=a;
//        x=-1;
        XYSeries series1 = new XYSeries("differences", true, true);
        XYSeries series2 = new XYSeries("full derivaty calculation", true, true);
        derivavty der = new derivavty(jMeshlessInterpolationFunction1D);
//        JCubicSpline st = new JCubicSpline(1);
        System.out.println("f([");
        while(x<b+0.001){
        	double y = der.calculate2(x);
//        	y = (st.evaluate(x+h) - 2*st.evaluate(x) + st.evaluate(x-h))/h/h;
//        	y = (st.evaluate(x+h) - st.evaluate(x-h))/h/2;
        	series1.add(x,y);
//        	series2.add(x,st.evaluateSecondDerivaty(x));
//        	series2.add(x,st.evaluateDerivaty(x));
        	series2.add(x,jMeshlessInterpolationFunction1D.calculateSecondDerivaty(x));
        	System.out.println(x+"\t\t"+(y));
            x+=0.01;
        }
        System.out.println("])");
        XYSeriesCollection col  = new XYSeriesCollection(series1);
        col.addSeries(series2);
        NewOkCancelDialog dialog = new NewOkCancelDialog(null, false, col);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        
//        ArrayList<FEMShapeFunction1D1> fem = new ArrayList<FEMShapeFunction1D1>();
////      fem.add(new FEMShapeFunction1D1(0.0, 1.0));
////      fem.add(new FEMShapeFunction1D1(1.0, 1.0));
////      fem.add(new FEMShapeFunction1D1(0.1, 0.1));
//      fem.add(new FEMShapeFunction1D1(0.0, 0.25));
////      fem.add(new FEMShapeFunction1D1(0.25, 0.25));
////      fem.add(new FEMShapeFunction1D1(0.5, 0.25));
////      fem.add(new FEMShapeFunction1D1(0.75, 0.25));
////      fem.add(new FEMShapeFunction1D1(1.0, 0.25));
//      double a = 0,b=1;
//      double x = a;
//
//
//      int nn = 5;
//      ArrayList<Particle> ls1D = new ArrayList<Particle>();
//      double h = (b-a)/(double)nn;
//      for (int i = 1; i <=nn; i++) {
//          ls1D.add(new Particle(a+h*i, 0, i));
//          //System.out.println(a+h*i);
//      }
//      ArrayList<JMeshlessInterpolationFunction1DCoupled2> ls = new ArrayList<JMeshlessInterpolationFunction1DCoupled2>();
//
////      for (int i = 1; i < ls1D.size()-1; i++) {
//      for (int i = 0; i < ls1D.size(); i++) {
////          JMeshlessInterpolationFunction1D f = new JMeshlessInterpolationFunction1D(1, ls1D.get(i).x, 2.6*h, new JGaussian());
//          JMeshlessInterpolationFunction1DCoupled2 f = new JMeshlessInterpolationFunction1DCoupled2(1, ls1D.get(i).getX(), 3.2*h, new JGaussian(), fem);
//          f.setNodeList(ls1D);
//          ls.add(f);
//      }
//
//
//      for (int i = 0; i < ls.size(); i++) {
//          JMeshlessInterpolationFunction1DCoupled2 f = ls.get(i);
//          x=0.0;
//          derivavty der = new derivavty(f);
//          System.out.println("f([");
//          while(x<b+0.01){
//              System.out.println(x+"\t\t"+der.calculate(x));
//              x+=0.01;
//          }
//          System.out.println("]);");
//      }

		
	}
	
}
